﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// VMI多基地库存查询
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class VMIStockInventoryInfoController : ControllerBase
    {
        private readonly ILogger<VMIStockInventoryInfoRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_qh;
        public readonly string con_yb;


        public VMIStockInventoryInfoController(ILogger<VMIStockInventoryInfoRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
            con= _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");
            //con = _configuration.GetConnectionString("WMSDbConnection_Test");

        }

        /// <summary>
        /// VMI多基地库存查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetVMIStockInfo([FromBody] VMIStockInventoryInfoRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            VMIStockInventoryInfoRequestDto result = new VMIStockInventoryInfoRequestDto();
            VMIStockInventoryInfoResponseDto re = new VMIStockInventoryInfoResponseDto();
            string organization_code = strparm.organization_code;
            string supplier_code = strparm.supplier_code;
            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            string strcon = "";
            if (organization_code == "E01")
            {
                strcon = con_yb;
            }
            else if (organization_code == "B01")
            {
                strcon = con_qh;
            }
            else if(organization_code == "C01")
            {
                strcon = con;
            }
            else
            {
                strMsg = "组织代码：C01珠海，B01青海，E01宜宾，请确认后再试！！！";
            }


            try
            {
                if (strMsg=="")
                {
                    strSql = new StringBuilder();
                    strSql.Append("select location_code from  base_location where  supplier_code='" + supplier_code + "' limit 1");
                    DataTable get_loc = DB.Query(strSql.ToString(), strcon);
                    if (get_loc.Rows.Count > 0)
                    {
                        strSql = new StringBuilder();
                        strSql.Append(@"select * from stock_inventory  where 1=1 and material_amount>0  and  location_code='" + get_loc.Rows[0]["location_code"].ToString().Trim() + "' ");
                        DataTable dt_st = DB.Query(strSql.ToString(), strcon);
                        if (dt_st.Rows.Count > 0)
                        {
                            re.data = DB.TableToListModel<VMIStockInventoryInfoResponseData>(dt_st);
                        }
                    }
                    else
                    {
                        if (supplier_code=="GK")
                        {
                            strSql = new StringBuilder();
                            strSql.Append(@"select st.* from stock_inventory  st  inner  join  base_location lo on 
st.location_code=lo.location_code  where 1=1 and st.material_amount>0  and  lo.supplier_code is not null");
                            DataTable dt_st = DB.Query(strSql.ToString(), strcon);
                            if (dt_st.Rows.Count > 0)
                            {
                                re.data = DB.TableToListModel<VMIStockInventoryInfoResponseData>(dt_st);
                            }
                        }
                        else
                        {
                            strMsg = "WMS库位档案，未配置当前供应商对照，请配置后再试！！！";
                        }
                       
                    }
                }                         
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
       
            if (strMsg=="")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }


            return new JsonResult(re);
        }





    }
}
